package com.sqfw.project.wpReport.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableSupport;
import com.sqfw.project.wp.domain.WpBackInfo;
import com.sqfw.project.wp.domain.WpInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wpReport.domain.WpInfoEvaluate;
import com.sqfw.project.wpReport.service.IWpInfoEvaluateService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 话务员流转工单评价表Controller
 *
 * @author lx
 * @date 2023-07-24
 */
@RestController
@RequestMapping("/wpReport/wpInfoEvaluate")
public class WpInfoEvaluateController extends BaseController {
    @Autowired
    private IWpInfoEvaluateService wpInfoEvaluateService;

    /**
     * 查询话务员流转工单评价表列表
     */

    @PreAuthorize("@ss.hasPermi('getWpInfoEvaluateList')")
    @GetMapping("/getWpInfoEvaluateList")
    public TableDataInfo getWpInfoEvaluateList(WpBackInfo wpBackInfo) {
        startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();


        List<WpInfoEvaluate> wpInfoEvaluates = wpInfoEvaluateService.getWpInfoEvaluateList(wpBackInfo);
        long total = getDataTable(wpInfoEvaluates).getTotal();
        if ((long) pageNum * pageSize >= total && total > 0) {
            List<WpInfoEvaluate> wpInfoEvaluateList = wpInfoEvaluateService.getWpInfoEvaluateList(wpBackInfo);
            WpInfoEvaluate wpInfoEvaluateCon = new WpInfoEvaluate();

            wpInfoEvaluateCon.setCode("总计");
            //扣分
            int deductPoint = wpInfoEvaluateList.stream()
                    .map(WpInfoEvaluate::getDeductPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setDeductPoint(deductPoint);
            //勾选错误
            int checkPoint = wpInfoEvaluateList.stream()
                    .map(WpInfoEvaluate::getCheckPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setCheckPoint(checkPoint);
            //格式错误
            int formatPoint = wpInfoEvaluateList.stream()
                    .map(WpInfoEvaluate::getFormatPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setFormatPoint(formatPoint);
            //过程不明确
            int processPoint = wpInfoEvaluateList.stream()
                    .map(WpInfoEvaluate::getProcessPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setProcessPoint(processPoint);

            //得分
            int countPoint = wpInfoEvaluateList.stream()
                    .map(WpInfoEvaluate::getCountPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setCountPoint(countPoint);
            wpInfoEvaluates.add(wpInfoEvaluateCon);

        }
        return getDataTable(wpInfoEvaluates);
    }

    /**
     * 导出话务员流转工单评价表列表
     */
    @Log(title = "话务员流转工单评价表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('getWpInfoEvaluateList')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WpBackInfo wpBackInfo) {
        List<WpInfoEvaluate> list = wpInfoEvaluateService.getWpInfoEvaluateList(wpBackInfo);

        if (list.size() > 0) {
            WpInfoEvaluate wpInfoEvaluateCon = new WpInfoEvaluate();

            wpInfoEvaluateCon.setCode("总计");
            //扣分
            int deductPoint = list.stream()
                    .map(WpInfoEvaluate::getDeductPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setDeductPoint(deductPoint);
            //勾选错误
            int checkPoint = list.stream()
                    .map(WpInfoEvaluate::getCheckPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setCheckPoint(checkPoint);
            //格式错误
            int formatPoint = list.stream()
                    .map(WpInfoEvaluate::getFormatPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setFormatPoint(formatPoint);
            //过程不明确
            int processPoint = list.stream()
                    .map(WpInfoEvaluate::getProcessPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setProcessPoint(processPoint);

            //得分
            int countPoint = list.stream()
                    .map(WpInfoEvaluate::getCountPoint)
                    .reduce(Integer::sum)
                    .orElse(0);
            wpInfoEvaluateCon.setCountPoint(countPoint);
            list.add(wpInfoEvaluateCon);

        }
        ExcelUtil<WpInfoEvaluate> util = new ExcelUtil<WpInfoEvaluate>(WpInfoEvaluate.class);
        util.exportExcel(response, list, "话务员流转工单评价表数据");
    }

    /**
     * 根据工单编号查询工单详情
     * @param wpid
     * @return
     */
    @PreAuthorize("@ss.hasPermi('getWpInfoDetailByWpid')")
    @GetMapping("/getWpInfoDetailByWpid")
    public AjaxResult getWpInfoDetail(String wpid){
        if (StringUtils.isEmpty(wpid)){
            return AjaxResult.error("工单编号不能为空，请确认后重新查询");
        }
        return wpInfoEvaluateService.getWpInfoDetail(wpid);

    }


}
